package com.example.larouzey.test02;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cbtService.AndroidSDK.ADCService;
import com.cbtService.AndroidSDK.BeepService;
import com.cbtService.AndroidSDK.LedService;
import com.example.larouzey.test02.R;

import org.w3c.dom.Text;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    LedService led_src;
    public static boolean iflag1 = true;

    private Button bt,bt1;
    private TextView tv;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            tv.setText(ADCService._adc_on()*(3.3/4096)+"V");
            if(ADCService._adc_on()*(3.3/4096)>=2){
                BeepService.PWMPlay(10);
                Timer timer = new Timer();
                TimerTask timerTask=new TimerTask() {
                    @Override
                    public void run() {
                        LedService._setLedState(0,1);
                    }
                };
                timer.schedule(timerTask,0,1000);
            }else{
                BeepService.PWMStop();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv=(TextView)findViewById(R.id.textView);
        bt=(Button)findViewById(R.id.button2);
        bt1=(Button)findViewById(R.id.button3);

        tv.setOnClickListener(this);
        bt.setOnClickListener(this);
        bt1.setOnClickListener(this);

        ADCService._init();
        LedService._init();
        BeepService._init();
    }
    public void onClick(View view){
        switch(view.getId()){
            case R.id.button2:
                LedService._setLedState(0,0);
                Timer timer = new Timer();
                TimerTask timerTask=new TimerTask() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        handler.sendMessage(msg);
                    }
                };
                timer.schedule(timerTask,0,1000);
                if(ADCService._adc_on()*(3.3/4096)>=2){
                    Timer timer1 = new Timer();
                    TimerTask timerTask1=new TimerTask() {
                        @Override
                        public void run() {
                            LedService._setLedState(0,1);
                        }
                    };
                    timer1.schedule(timerTask1,0,1000);
                }else{
                    LedService._setLedState(0,0);
                }
                break;
            case R.id.button3:
                LedService._setLedState(0,0);
                BeepService.PWMStop();
                break;
        }
    }
}